package com.lysj.bhp.slideshow.controller;

import com.lysj.bhp.article.domain.Article;
import com.lysj.bhp.exchangerate.json.bcex.Data;
import com.lysj.bhp.slideshow.base.SlideshowContent;
import com.lysj.bhp.slideshow.domin.Slideshow;
import com.lysj.bhp.util.random.FormatUtil;
import com.lysj.bhp.util.web.ParamUtil;
import com.lysj.bhp.util.web.Resp;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @Author: ZhangWenJian
 * @Create: 2018/8/7--10:51
 * @Description: 轮播图控制类
 */
@RestController
@RequestMapping("admin/slideshow")
public class SlideshowController extends SlideshowContent {

    @GetMapping("/list")
    public Resp list() {
        return new Resp(slideshowRepository.findByStatusNotOrderByMapOrderDesc(-1));
    }

    @PostMapping("/add")
    public Resp add(Slideshow model){
        if(model.getChineseTitle() == null || ParamUtil.isBlack(model.getChineseTitle() )){
            return new Resp(Resp.PARAM_ERROR, "请填写中文标题");
        }
        if(model.getEnglishTitle() == null || ParamUtil.isBlack(model.getEnglishTitle())){
            return new Resp(Resp.PARAM_ERROR, "请填写英文标题");
        }
        if(model.getChineseExplain() == null || ParamUtil.isBlack(model.getChineseExplain())){
            return new Resp(Resp.PARAM_ERROR, "请填写中文简介");
        }
        if(model.getEnglishExplain() == null || ParamUtil.isBlack(model.getEnglishExplain())){
            return new Resp(Resp.PARAM_ERROR, "请填写英文简介");
        }
        if(model.getChineseBigMap() == null || ParamUtil.isBlack(model.getChineseBigMap())){
            return new Resp(Resp.PARAM_ERROR,"请上传大分辨率图片（中文）");
        }
        if(model.getChineseBigMap().indexOf("-") != -1){
            return new Resp(Resp.PARAM_ERROR,"大分辨率图片（中文）只能够上传1张！请移除对应图片后重试！");
        }
        if(model.getChineseSmallMap() == null || ParamUtil.isBlack(model.getChineseSmallMap())){
            return new Resp(Resp.PARAM_ERROR,"请上传小分辨率图片（中文）");
        }
        if(model.getChineseSmallMap().indexOf("-") != -1){
            return new Resp(Resp.PARAM_ERROR,"小分辨率图片（中文）只能够上传1张！请移除对应图片后重试！");
        }
        if(model.getEnglishBigMap() == null || ParamUtil.isBlack(model.getEnglishBigMap())){
            return new Resp(Resp.PARAM_ERROR,"请上传大分辨率图片（英文）");
        }
        if(model.getEnglishBigMap().indexOf("-") != -1){
            return new Resp(Resp.PARAM_ERROR,"大分辨率图片（英文）只能够上传1张！请移除对应图片后重试！");
        }
        if(model.getEnglishSmallMap() == null || ParamUtil.isBlack(model.getEnglishSmallMap())){
            return new Resp(Resp.PARAM_ERROR,"请上传小分辨率图片（英文）");
        }
        if(model.getEnglishSmallMap().indexOf("-") != -1){
            return new Resp(Resp.PARAM_ERROR,"小分辨率图片（英文）只能够上传1张！请移除对应图片后重试！");
        }

        if(model.getJump() == null || ParamUtil.isBlack(model.getJump())){
            return new Resp(Resp.PARAM_ERROR,"请选择跳转方式");
        }
        if(model.getMapOrder() == null || ParamUtil.isBlack(model.getMapOrder())){
            return new Resp(Resp.PARAM_ERROR,"请输入排序值！");
        }
        if(model.getJump() == 1 ){
            if(model.getEnglishInteriorLink() == null || ParamUtil.isBlack(model.getEnglishInteriorLink())){
                return new Resp(Resp.PARAM_ERROR,"请填写英文内链URL");
            }
            if(model.getChineseInteriorLink() == null || ParamUtil.isBlack(model.getChineseInteriorLink())){
                return new Resp(Resp.PARAM_ERROR,"请填写中文内链URL");
            }
        }
        if(model.getJump() == 2 ){
            if(model.getEnglishExternalLink() == null || ParamUtil.isBlack(model.getEnglishExternalLink())){
                return new Resp(Resp.PARAM_ERROR,"请填写英文外链URL");
            }
            if(model.getEnglishExternalLink().indexOf("https://") == -1){
                return new Resp(Resp.PARAM_ERROR,"请填写准确英文外链URL，需要加上https://");
            }
            if(model.getChineseExternalLink() == null || ParamUtil.isBlack(model.getChineseExternalLink())){
                return new Resp(Resp.PARAM_ERROR,"请填写中文外链URL");
            }
            if(model.getChineseExternalLink().indexOf("https://") == -1){
                return new Resp(Resp.PARAM_ERROR,"请填写准确中文外链URL，需要加上https://");
            }
        }
        model.setStatus(1);//设置新添加图片状态为 激活状态
        model.setCreateTime(new Date());//创建时间
        model.setUpdateTime(new Date());//更新时间
        slideshowRepository.save(model);//保存
        return new Resp("添加成功！");
    }
    @PostMapping("/edit")
    public Resp edit(Slideshow model) {
        Slideshow slideshow = slideshowRepository.findOne(model.getSlideshowId());
        if(slideshow == null){
            return new Resp(Resp.PARAM_ERROR, "查无此轮播图");
        }
        if(model.getChineseTitle() == null || ParamUtil.isBlack(model.getChineseTitle() )){
            return new Resp(Resp.PARAM_ERROR, "请填写中文标题");
        }
        if(model.getEnglishTitle() == null || ParamUtil.isBlack(model.getEnglishTitle())){
            return new Resp(Resp.PARAM_ERROR, "请填写英文标题");
        }
        if(model.getChineseExplain() == null || ParamUtil.isBlack(model.getChineseExplain())){
            return new Resp(Resp.PARAM_ERROR, "请填写中文简介");
        }
        if(model.getEnglishExplain() == null || ParamUtil.isBlack(model.getEnglishExplain())){
            return new Resp(Resp.PARAM_ERROR, "请填写英文简介");
        }
        if(model.getChineseBigMap() == null || ParamUtil.isBlack(model.getChineseBigMap())){
            return new Resp(Resp.PARAM_ERROR,"请上传大分辨率图片（中文）");
        }
        if(model.getChineseBigMap().indexOf("-") != -1){
            return new Resp(Resp.PARAM_ERROR,"大分辨率图片（中文）只能够上传1张！请移除对应图片后重试！");
        }
        if(model.getChineseSmallMap() == null || ParamUtil.isBlack(model.getChineseSmallMap())){
            return new Resp(Resp.PARAM_ERROR,"请上传小分辨率图片（中文）");
        }
        if(model.getChineseSmallMap().indexOf("-") != -1){
            return new Resp(Resp.PARAM_ERROR,"小分辨率图片（中文）只能够上传1张！请移除对应图片后重试！");
        }
        if(model.getEnglishBigMap() == null || ParamUtil.isBlack(model.getEnglishBigMap())){
            return new Resp(Resp.PARAM_ERROR,"请上传大分辨率图片（英文）");
        }
        if(model.getEnglishBigMap().indexOf("-") != -1){
            return new Resp(Resp.PARAM_ERROR,"大分辨率图片（英文）只能够上传1张！请移除对应图片后重试！");
        }
        if(model.getEnglishSmallMap() == null || ParamUtil.isBlack(model.getEnglishSmallMap())){
            return new Resp(Resp.PARAM_ERROR,"请上传小分辨率图片（英文）");
        }
        if(model.getEnglishSmallMap().indexOf("-") != -1){
            return new Resp(Resp.PARAM_ERROR,"小分辨率图片（英文）只能够上传1张！请移除对应图片后重试！");
        }
        if(model.getJump() == 1 ){
            if(model.getEnglishInteriorLink() == null || ParamUtil.isBlack(model.getEnglishInteriorLink())){
                return new Resp(Resp.PARAM_ERROR,"请填写英文内链URL");
            }
            if(model.getChineseInteriorLink() == null || ParamUtil.isBlack(model.getChineseInteriorLink())){
                return new Resp(Resp.PARAM_ERROR,"请填写中文内链URL");
            }
        }
        if(model.getJump() == 2 ){
            if(model.getEnglishExternalLink() == null || ParamUtil.isBlack(model.getEnglishExternalLink())){
                return new Resp(Resp.PARAM_ERROR,"请填写英文外链URL");
            }
            if(model.getEnglishExternalLink().indexOf("https://") == -1){
                return new Resp(Resp.PARAM_ERROR,"请填写准确英文外链URL，需要加上https://");
            }
            if(model.getChineseExternalLink() == null || ParamUtil.isBlack(model.getChineseExternalLink())){
                return new Resp(Resp.PARAM_ERROR,"请填写中文外链URL");
            }
            if(model.getChineseExternalLink().indexOf("https://") == -1){
                return new Resp(Resp.PARAM_ERROR,"请填写准确中文外链URL，需要加上https://");
            }
        }
        model.setUpdateTime(new Date());//更新时间
        model.setStatus(slideshow.getStatus());
        model.setCreateTime(slideshow.getCreateTime());
        slideshowRepository.save(model);//再次保存
        return new Resp("修改成功！");
    }


    @PostMapping("/remove")
    public Resp remove(Slideshow model) {
        if (ParamUtil.isBlack(model.getSlideshowId())) {
            return new Resp(Resp.PARAM_ERROR, "请选择需要删除的轮播图");
        }
        Slideshow slideshow = slideshowRepository.findOne(model.getSlideshowId());
        if (null == slideshow) {
            return new Resp(Resp.PARAM_ERROR, "查无此轮播图");
        }
        slideshow.setStatus(-1); //设定状态为-1,用于逻辑删除
        slideshow.setUpdateTime(new Date());//刷新更新时间
        slideshowRepository.save(slideshow);
        return new Resp("删除成功");
    }

    @PostMapping("/changeStatus")
    public Resp changeStatus(Slideshow model) {
        System.out.println(123);
        if (ParamUtil.isBlack(model.getSlideshowId())) {
            return new Resp(Resp.PARAM_ERROR, "请选择需要删除的轮播图");
        }
        Slideshow slideshow = slideshowRepository.findOne(model.getSlideshowId());
        if (null == slideshow) {
            return new Resp(Resp.PARAM_ERROR, "查无此轮播图,或已被删除");
        }
        if(model.getStatus() == 1 ){
            slideshow.setStatus(2); //设定状态为2,用于不激活状态
        }
        else if(model.getStatus() == 2){
            slideshow.setStatus(1); //设定状态为1,用于激活状态
        }
        slideshow.setUpdateTime(new Date());//刷新更新时间
        slideshowRepository.save(slideshow);
        return new Resp("更改成功");
    }


}
